package com.book.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.book.entity.User;
import com.book.request.GetUserReq;
import com.book.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import utils.SecurityUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    @RequestMapping("/admin/list")
    public String adminList(ModelMap m){
        if(SecurityUtil.getCurrentRole()!=null &&"admin".equals(SecurityUtil.getCurrentRole().getName())){
            return "/admin/user/list";
        }else{
            m.put("errorMsg","权限不足，只有管理员才能访问此页面");
            return "common/500";
        }
    }

    @RequestMapping("/getPage")
    public Page getPage(GetUserReq getUserReq){
        return userService.getUsers(getUserReq);
    }

    @RequestMapping("/regist")
    public ModelAndView regist(User user){
        ModelAndView mav=new ModelAndView();
        userService.add(user);
        msg.setSuccessMsg("注册成功");
        mav.addObject("msg",msg);
        mav.setViewName("registSuccess");
        return mav;
    }

    @RequestMapping("/validate_username")
    public Map<String,Object> checkUsername(HttpServletRequest request){
        String username=get(request,"username");
        Map<String,Object> resultMap=new HashMap<>();
        User user=userService.findUserByUserName(username);
        if (user==null){
            resultMap.put("flag",true);
        }else {
            resultMap.put("flag",false);
        }
        return resultMap;
    }
    @RequestMapping("/admin/grant")
    public Map<String,Object> grant(HttpServletRequest request){
        String uid=get(request, "uId");
        Map<String,Object> resultMap=new HashMap<String,Object>();
        if(StringUtils.isEmpty(uid)){
            msg.setErrorMsg("设置失败");
            resultMap.put("msg", msg);
            return resultMap;
        }
        try{
            userService.grantAdmin(uid);
            msg.setSuccessMsg("设置成功");
        }catch(Exception e){
            msg.setErrorMsg("设置失败");
            e.printStackTrace();
        }
        resultMap.put("msg", msg);
        return resultMap;
    }
    @RequestMapping("/to_regist")
    public ModelAndView toRegist(){
        ModelAndView mav=new ModelAndView();
        mav.setViewName( "regist");
        return mav;
    }
}
